// pages/shebei/shebei.js
Page({
  data: {
    devices: [
      {
        deviceId: 'device_001',
        name: '设备1',
        connected: false,
        connecting: false
      },
      {
        deviceId: 'device_002',
        name: '设备2',
        connected: false,
        connecting: false
      },
      {
        deviceId: 'device_003',
        name: '设备3',
        connected: false,
        connecting: false
      }
    ]
  },

  onLoad() {
    this.checkConnectedDevices();
  },

  checkConnectedDevices() {
    const connectedDeviceId = wx.getStorageSync('connectedDeviceId');
    if (connectedDeviceId) {
      const updatedDevices = this.data.devices.map(device => {
        return {
          ...device,
          connected: device.deviceId === connectedDeviceId,
          connecting: false
        }
      });
      this.setData({ devices: updatedDevices });
    }
  },

  connectDevice(e) {
    const device = e.currentTarget.dataset.device;
    const deviceId = device.deviceId;
    
    const updatedDevices = this.data.devices.map(d => {
      if (d.deviceId === deviceId) {
        return { ...d, connecting: true };
      }
      return { ...d, connected: false };
    });
    this.setData({ devices: updatedDevices });
    
    setTimeout(() => {
      this.handleConnectResult(deviceId, true);
    }, 1500);
  },

  handleConnectResult(deviceId, success) {
    const updatedDevices = this.data.devices.map(d => {
      if (d.deviceId === deviceId) {
        return {
          ...d,
          connecting: false,
          connected: success
        };
      }
      return d;
    });
    
    this.setData({ devices: updatedDevices });
    
    if (success) {
      wx.setStorageSync('connectedDeviceId', deviceId);
      wx.showToast({
        title: '连接成功',
        icon: 'success',
        duration: 1500,
        success: () => {
          setTimeout(() => {
            wx.reLaunch({ url: '/pages/zhuye/zhuye' });
          }, 1500);
        }
      });
    } else {
      wx.showToast({ title: '连接失败', icon: 'none' });
    }
  },

  deleteDevice(deviceId) {
    const connectedDeviceId = wx.getStorageSync('connectedDeviceId');
    if (connectedDeviceId === deviceId) {
      wx.removeStorageSync('connectedDeviceId');
    }
    
    const updatedDevices = this.data.devices.filter(d => d.deviceId !== deviceId);
    this.setData({ devices: updatedDevices });
    
    wx.showToast({
      title: '删除成功',
      icon: 'success',
      duration: 1500,
      success: () => {
        setTimeout(() => {
          wx.reLaunch({ url: '/pages/zhuye/zhuye' });
        }, 1500);
      }
    });
  },

  onUnload() {
    wx.reLaunch({
      url: '/pages/zhuye/zhuye'
    });
  },

  showDeleteConfirm(e) {
    const deviceId = e.currentTarget.dataset.deviceId;
    const device = this.data.devices.find(d => d.deviceId === deviceId);
    
    wx.showModal({
      title: '确认删除',
      content: `确定要删除"${device.name}"吗？`,
      success: (res) => {
        if (res.confirm) {
          this.deleteDevice(deviceId);
        }
      }
    });
  }
});